package com.xm.xmapp.fragment;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.xm.xmapp.PoiKeywordSearchActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.myinterface.MainInterFaceAble;
import com.xm.xmapp.tools.AndroidTool;
import com.xm.xmapp.tools.Tools;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by LDW on 2017/7/19.
 */
public class MainTopFragment extends Fragment implements TabLayout.OnTabSelectedListener {


    @Bind(R.id.iv_person)
    ImageView ivPerson;
    @Bind(R.id.tl_tab)
    TabLayout tlTab;
    @Bind(R.id.iv_search)
    ImageView ivSearch;

    private MainInterFaceAble mMainInterFaceAble;

    public void setmMainInterFaceAble(MainInterFaceAble mMainInterFaceAble) {
        this.mMainInterFaceAble = mMainInterFaceAble;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_maintop, null, false);
        ButterKnife.bind(this, view);
        initImg(false);
        tlTab.addOnTabSelectedListener(this);
        return view;
    }

    /**
     * 是否是大客户页面
     *
     * @param isBigCustomer
     */
    private void initImg(boolean isBigCustomer) {
        if (isBigCustomer) {

            tlTab.removeAllTabs();
            tlTab.addTab(tlTab.newTab().setText("企业"),true);
            tlTab.addTab(tlTab.newTab().setText("电桩"));
            tlTab.post(new Runnable() {
                @Override
                public void run() {
                    int marig = Tools.dp2px(10);
                    AndroidTool.setIndicator(tlTab,marig,marig);
                }
            });


        } else {

            tlTab.removeAllTabs();
            tlTab.addTab(tlTab.newTab().setText("分时"),true);
            tlTab.addTab(tlTab.newTab().setText("长时"));
            tlTab.addTab(tlTab.newTab().setText("电桩"));
            tlTab.post(new Runnable() {
                @Override
                public void run() {
                    int marig = Tools.dp2px(5);
                    AndroidTool.setIndicator(tlTab,marig,marig);
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({ R.id.iv_person, R.id.iv_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_person:
                mMainInterFaceAble.uIChange(MainInterFaceAble.SHOW_LEFT, null);
                break;
            case R.id.iv_search: {
                Intent intent = new Intent(MyAppcation.getMyAppcation(), PoiKeywordSearchActivity.class);
                startActivityForResult(intent, CodeConstant.AddressCode);
                break;
            }


        }
    }



    /**
     * 大客户切换
     */
    public void setBigCustomer(boolean isBigCustomer) {
        initImg(isBigCustomer);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        if(tlTab.getTabCount()>2) {
            mMainInterFaceAble.uIChange(MainInterFaceAble.NAVIGATION, tab.getPosition());
        } else {
            if(tab.getPosition()==0)
                mMainInterFaceAble.uIChange(MainInterFaceAble.NAVIGATION, 3);
            else
                mMainInterFaceAble.uIChange(MainInterFaceAble.NAVIGATION, 2);
        }

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
